﻿using Solution.Business.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Solution.UI.Web.Controllers.Admin
{
    public class ThongkeController : Controller
    {
        private SolutionEntities db = new SolutionEntities();
        //
        // GET: /Thongke/

        public ActionResult Index()
        {
            CreateStatistics();
            List<Thongke> thongkeList = new List<Thongke>();
            DateTime date1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            thongkeList = db.Thongkes.Where(c => c.Month == DateTime.Now.Month && c.Transaction.Level.Length > 4).ToList();
            return View(thongkeList);
        }

         public void CreateStatistics()
        {
            //DateTime currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);            
            if (db.Thongkes.Where(c => c.Month == DateTime.Now.Month).Count() == 0)
            {
                foreach (var item in db.Transactions.ToList())
                {
                    Thongke statistics = new Thongke();
                    statistics.Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                    statistics.Month = DateTime.Now.Month;
                    statistics.TrId = item.TrId;
                    statistics.Count = 0;
                    db.Thongkes.Add(statistics);
                    db.SaveChanges();
                }
            }
        }
    }
}
